﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Bus_proj.website
{
    public partial class check_24Hour : System.Web.UI.Page
    {
        string date, time, date2;
        int check;
        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (years2 + "/" + mount2 + "/" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int around = Int32.Parse(Session["data_AroundID"].ToString());
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            string point = @"select Table_Around.Around_date,Table_Time.Time_time
                            from Table_Around
                            inner join Table_Time
                                on Table_Around.Time_id = Table_Time.Time_id
                            where Around_date >= DATEADD(HOUR,24,GETDATE()) and Table_Around.Around_id = " + around + "";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(point, sqlconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count <= 0)
            {
                Response.Redirect("Passenger_sale_ticket.aspx");
            }
            else
            {
                try
                {
                    date = ConvestToDate_check(dt.Rows[0]["Around_date"].ToString());
                    time = dt.Rows[0]["Time_time"].ToString();
                    Response.Redirect("Passenger_sale_seat.aspx");
                    //ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('NOOOOOOOOOOOO');", true);
                }
                catch (Exception)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('YESSSSs');", true);
                }
            }
            
            //date2 = date + " " + time;
            //using (SqlConnection con = new SqlConnection(Connection))
            //{
            //    using (SqlCommand cmd = new SqlCommand("TESTLOGIN", con))
            //    {
            //        cmd.CommandType = CommandType.StoredProcedure;
            //        cmd.Parameters.Add(new SqlParameter("@start", date2.ToString().Trim()));
            //        SqlParameter returnParameter = cmd.Parameters.Add("@start", SqlDbType.VarChar);
            //        returnParameter.Direction = ParameterDirection.ReturnValue;
            //        con.Open();
            //        cmd.ExecuteNonQuery();
            //        check = (int)returnParameter.Value;
            //    }
            //}
            //if (check > 1440)
            //{
            //    Response.Redirect("Passenger_sale_seat.aspx");
            //}
            //else
            //{
            //    //Session["alert"] = 1;
            //    Response.Redirect("Passenger_sale_ticket.aspx"); 
            //}            
        }
    }
}